
package com.anjiplus.template.gaea.business.modules.httpdatasettransform.service.impl;

import com.anji.plus.gaea.curd.mapper.GaeaBaseMapper;
import com.anjiplus.template.gaea.business.modules.httpdatasettransform.controller.dto.HttpDataSetTransformDto;
import com.anjiplus.template.gaea.business.modules.httpdatasettransform.dao.HttpDataSetTransformMapper;
import com.anjiplus.template.gaea.business.modules.httpdatasettransform.dao.entity.HttpDataSetTransform;
import com.anjiplus.template.gaea.business.modules.httpdatasettransform.service.HttpDataSetTransformService;
import com.anjiplus.template.gaea.business.modules.httptotransform.dao.entity.HttpToTransform;
import com.anjiplus.template.gaea.business.modules.httptotransform.service.HttpToTransformService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Raod
 * @desc Http 数据集服务实现
 * @date 2021-03-18 12:11:31.150755900
 **/
@Service
//@RequiredArgsConstructor
@Slf4j
public class HttpDataSetTransformServiceImpl implements HttpDataSetTransformService {

    @Autowired
    private HttpDataSetTransformMapper HttpDataSetTransformMapper;
    @Autowired
    private HttpToTransformService httpToTransformService;

    @Override
    public GaeaBaseMapper<HttpDataSetTransform> getMapper() {
        return HttpDataSetTransformMapper;
    }


    @Override
    public List<HttpDataSetTransformDto> getByHttpId(String httpId) {
        if (StringUtils.isEmpty(httpId)) {
            return null;
        }
        //1.httpId查中间表
        List<HttpToTransform> temp = httpToTransformService.getByHttpId(httpId);
        if (CollectionUtils.isEmpty(temp)) {
            return null;
        }
        List<HttpDataSetTransformDto> results = new ArrayList<>();

        for (HttpToTransform httpToTransform : temp) {
            LambdaQueryWrapper<HttpDataSetTransform> wrapper = new LambdaQueryWrapper<>();
            String setTransFromId = httpToTransform.getSetTransformId();
            if (StringUtils.isEmpty(setTransFromId)) {
                continue;
            }
            wrapper.eq(HttpDataSetTransform::getId, setTransFromId);
            HttpDataSetTransform httpDataSetTransform = HttpDataSetTransformMapper.selectOne(wrapper);

            if (null != httpDataSetTransform) {
                HttpDataSetTransformDto dto = new HttpDataSetTransformDto();
                BeanUtils.copyProperties(httpDataSetTransform, dto);
                results.add(dto);
            }

        }
        return results;
    }

    @Override
    public Integer updateById(List<HttpDataSetTransformDto> list) {
        for (HttpDataSetTransformDto HttpDataSetTransformDto : list) {
            HttpDataSetTransform HttpDataSetTransform = new HttpDataSetTransform();
            BeanUtils.copyProperties(HttpDataSetTransformDto, HttpDataSetTransform);
            HttpDataSetTransformMapper.updateById(HttpDataSetTransform);
        }
        return 1;
    }



}
